<template>
	<view>
		<!--导航栏-->
		<pageLayout
			:barTitle="title"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="调拨单ID">
							<uni-easyinput v-model="searchModel.warehouseAllocationId" placeholder="请输入调拨单ID" />
						</uni-forms-item>
						<uni-forms-item label="商品ID">
							<uni-easyinput v-model="searchModel.productId" placeholder="请输入商品ID" />
						</uni-forms-item>
						<uni-forms-item label="商品名称">
							<uni-easyinput v-model="searchModel.productName" placeholder="请输入商品名称" />
						</uni-forms-item>
						<uni-forms-item label="商品规格">
							<uni-easyinput v-model="searchModel.productSpecName" placeholder="请输入商品规格" />
						</uni-forms-item>
						<uni-forms-item label="规格ID">
							<uni-easyinput v-model="searchModel.productSpecId" placeholder="请输入规格ID" />
						</uni-forms-item>
						<uni-forms-item label="生产日期">
							<uni-datetime-picker type="date" placeholder="请选择生产日期" v-model="searchModel.productionDate"/>
						</uni-forms-item>
						<uni-forms-item label="过期时间">
							<uni-datetime-picker type="date" placeholder="请选择过期时间" v-model="searchModel.expireDate"/>
						</uni-forms-item>
						<uni-forms-item label="采购单价">
							<uni-easyinput v-model="searchModel.price" placeholder="请输入采购单价" />
						</uni-forms-item>
						<uni-forms-item label="商品数量">
							<uni-easyinput v-model="searchModel.number" placeholder="请输入商品数量" />
						</uni-forms-item>
						<uni-forms-item label="批次号">
							<uni-easyinput v-model="searchModel.batchNumber" placeholder="请输入批次号" />
						</uni-forms-item>
						<uni-forms-item label="采购价格">
							<uni-easyinput v-model="searchModel.price2" placeholder="请输入采购价格" />
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							form-type="reset"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
		</up-popup>
		<view class="content">
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					@row-click="rowClick"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					stripe
					border>
					<template v-slot:left="{ row }">
						<view class="table-td-cell">
							<!-- <view class="table-td-row">ID：<text class="table-td-text">{{row.ID}}</text></view> -->
							<view class="table-td-row">调拨单ID：<text class="table-td-text">{{row.formWarehouseName}}</text></view>
							<view class="table-td-row">商品ID：<text class="table-td-text">{{row.formWarehouseName}}</text></view>
							<view class="table-td-row">商品名称：<text class="table-td-text">{{row.destinationWarehouseName}}</text></view>
							<view class="table-td-row">批次号：<text class="table-td-text">{{row.destinationWarehouseName}}</text></view>
						</view>
					</template>
					<template v-slot:right="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">规格ID：<text class="table-td-text">{{row.destinationWarehouseName}}</text></view>
							<view class="table-td-row">规格名称：<text class="table-td-text">{{row.destinationWarehouseName}}</text></view>
							<view class="table-td-row">生产日期：<text class="table-td-text">{{row.createTime}}</text></view>
							<view class="table-td-row">过期时间：<text class="table-td-text">{{row.sendTime}}</text></view>
							<view class="table-td-row">采购单价：<text class="table-td-text">{{row.arriveTime}}</text></view>
							<view class="table-td-row">商品数量：<text class="table-td-text">{{row.remark}}</text></view>
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref } from 'vue'
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { allocationStatus } from "@/api/system/dict/data"
import { warehouseAllocationRecord } from "@/api/erp_api/warehouse"
import { isObjectEmptySimple, copyText, arrText } from '@/utils/ty.js';
import useUserStore from '@/store/modules/user'

	const userStore = useUserStore()
	// 标题设置
	const title = ref('')
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		uni.setNavigationBarTitle({
			title: title.value 
		})
	})
	
	// 仓库调拨状态
	const allStatusList = ref([])
	const getAllocationStatus = () => {
		return allocationStatus().then(res => {
			allStatusList.value = res.data.map(item => {
				return {
					...item,
					value: item.dictValue,
					text: item.dictLabel
				}
			}) || []
		})
	}
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		warehouseAllocationId: '',
		productId: '',
		productName: '',
		productSpecId: '',
		productSpecName: '',
		productionDate: '',
		expireDate: '',
		price: '',
		number: '',
		batchNumber: '',
		price2: ''
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			warehouseAllocationId: '',
			productId: '',
			productName: '',
			productSpecId: '',
			productSpecName: '',
			productionDate: '',
			expireDate: '',
			price: '',
			number: '',
			batchNumber: '',
			price2: ''
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '商品信息', key: 'left' },
		{ title: '数量价格', key: 'right' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		if (searchModel.value.warehouseAllocationId) {
			params.warehouseAllocationId = searchModel.value.warehouseAllocationId
		}
		if (searchModel.value.productId) {
			params.productId = searchModel.value.productId
		}
		if (searchModel.value.productName) {
			params.productName = searchModel.value.productName
		}
		if (searchModel.value.productSpecId) {
			params.productSpecId = searchModel.value.productSpecId
		}
		if (searchModel.value.productSpecName) {
			params.productSpecName = searchModel.value.productSpecName
		}
		if (searchModel.value.productionDate) {
			params.productionDate = searchModel.value.productionDate
		}
		if (searchModel.value.expireDate) {
			params.expireDate = searchModel.value.expireDate
		}
		if (searchModel.value.price) {
			params.price = searchModel.value.price
		}
		if (searchModel.value.number) {
			params.number = searchModel.value.number
		}
		if (searchModel.value.batchNumber) {
			params.batchNumber = searchModel.value.batchNumber
		}
		if (searchModel.value.price2) {
			params.price2 = searchModel.value.price2
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		warehouseAllocationRecord(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 点击行
	const rowClick = (row) => {
		uni.navigateTo({
			url: `/pages/warehouse/warehouseAllocationRecordInfo?id=${row.id}`
		})
	}
	
	onMounted(async () => {
		await getAllocationStatus()
		searchFirst()
	})
</script>

<style scoped>
.form {
	background-color: #fff;
}
.record-type-0 {
	background-color: #e7faf0;
	border-color: #d0f5e0;
	color: #13ce66;
	padding: 2px 6px !important;
}
.record-type-1 {
	background-color: #ffeded;
	border-color: #ffdbdb;
	color: #ff4949;
	padding: 2px 6px !important;
}
</style>
